import 'package:flutter/material.dart';

import 'package:get/get.dart';
import 'package:getx_learn/app/common/widgets/widgets.dart';

import '../controllers/anti_shake_controller.dart';

class AntiShakeView extends GetView<AntiShakeController> {
  const AntiShakeView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: getAppBar(context: context, title: "防抖、限流"),
      body: ListView(
        children: [
          Text("""
            // 监听count的值,当它发生改变的时候调用
            ever(count, (callback) => print("ever----\$count"));
          
            // 监听多个值,当它们发生改变的时候调用
            everAll([count], (callback) => print("everAll----\$count"));
          
            // count值改变时调用,只执行一次
            once(count, (callback) => print("once----\$count"));
          
            // 用户停止打字时1秒后调用,主要是防DDos
            debounce(count, (callback) => print("debounce----\$count"));
          
            // 忽略3秒内的所有变动
            interval(count, (callback) => print("interval----\$count"));
            
            
            
            ever 适合做监听、日志收集

            debounce 适合做搜索输入框
          """),
          Divider(),
          ListTile(
            title: Text("ever 监听count的值"),
            subtitle: Text("每次都会回调"),
            onTap: () {
              controller.setType(1);
            },
          ),
          Divider(),
          ListTile(
            title: Text("ever 监听多个值"),
            subtitle: Text("每次都会回调"),
            onTap: () {
              controller.setType(2);
            },
          ),
          Divider(),
          ListTile(
            title: Text("once 监听count的值"),
            subtitle: Text("值改变时调用,只执行一次（就是第一次）"),
            onTap: () {
              controller.setType(3);
            },
          ),
          Divider(),
          ListTile(
            title: Text("debounce 监听count的值"),
            subtitle: Text("防抖，用户停止打字时1秒后调用,主要是防DDos"),
            onTap: () {
              controller.setType(4);
            },
          ),
          Divider(),
          ListTile(
            title: Text("interval 监听count的值"),
            subtitle: Text("忽略n秒内的所有变动（定时器）"),
            onTap: () {
              controller.setType(5);
            },
          ),
          Divider(),
          ElevatedButton(
            onPressed: (){
              controller.increment();
            },
            child: Text("+1"),
          ),

          ElevatedButton(
            onPressed: (){
              controller.changeName();
            },
            child: Text("改变名称"),
          ),
        ],
      ),
    );
  }
}
